package com.erp.wallet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.wallet.domain.ErpSellerStoreAccountLog;
import com.erp.wallet.domain.bo.ErpSellerStoreAccountLogBo;
import com.erp.wallet.domain.vo.ErpSellerStoreAccountLogVo;
import com.erp.wallet.mapper.ErpSellerStoreAccountLogMapper;
import com.erp.wallet.service.IErpSellerStoreAccountLogService;
import com.erp.wallet.service.IErpSellerStoreWalletService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 手工账日志记录Service业务层处理
 *
 * @author erp
 * @date 2023-05-31
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStoreAccountLogServiceImpl implements IErpSellerStoreAccountLogService {

	private final ErpSellerStoreAccountLogMapper baseMapper;
	private final IErpSellerStoreWalletService iErpSellerStoreWalletService;

	/**
	 * 查询手工账日志记录
	 */
	@Override
	public ErpSellerStoreAccountLogVo queryById(Long id) {
		return baseMapper.selectVoById(id);
	}

	/**
	 * 查询手工账日志记录列表
	 */
	@Override
	public TableDataInfo<ErpSellerStoreAccountLogVo> queryPageList(ErpSellerStoreAccountLogBo bo, PageQuery pageQuery) {
		LambdaQueryWrapper<ErpSellerStoreAccountLog> lqw = buildQueryWrapper(bo);
		Page<ErpSellerStoreAccountLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
		return TableDataInfo.build(result);
	}

	/**
	 * 查询手工账日志记录列表
	 */
	@Override
	public List<ErpSellerStoreAccountLogVo> queryList(ErpSellerStoreAccountLogBo bo) {
		LambdaQueryWrapper<ErpSellerStoreAccountLog> lqw = buildQueryWrapper(bo);
		return baseMapper.selectVoList(lqw);
	}

	private LambdaQueryWrapper<ErpSellerStoreAccountLog> buildQueryWrapper(ErpSellerStoreAccountLogBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<ErpSellerStoreAccountLog> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getFlowType() != null, ErpSellerStoreAccountLog::getFlowType, bo.getFlowType());
		lqw.eq(bo.getExpenseType() != null, ErpSellerStoreAccountLog::getExpenseType, bo.getExpenseType());
		lqw.eq(bo.getPayType() != null, ErpSellerStoreAccountLog::getPayType, bo.getPayType());
		lqw.eq(bo.getSellerId() != null, ErpSellerStoreAccountLog::getSellerId, bo.getSellerId());
		lqw.eq(bo.getAmount() != null, ErpSellerStoreAccountLog::getAmount, bo.getAmount());
		lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), ErpSellerStoreAccountLog::getOrderNo, bo.getOrderNo());
		return lqw;
	}

	/**
	 * 新增手工账日志记录
	 */
	@Override
	@Transactional
	public Boolean insertByBo(ErpSellerStoreAccountLogBo bo) {
		ErpSellerStoreAccountLog add = BeanUtil.toBean(bo, ErpSellerStoreAccountLog.class);
		validEntityBeforeSave(add);
		// 创建订单号
		add.setOrderNo("SG" + new Date().getTime());
		boolean flag = baseMapper.insert(add) > 0;
		if (flag) {
			bo.setId(add.getId());
			// 修改钱包的金额
			iErpSellerStoreWalletService.updateWallet(add.getFlowType(), add.getExpenseType(), add.getPayType(),
					add.getAmount(), add.getSellerId(), add.getOrderNo(), add.getRemark());
		}
		return flag;
	}

	/**
	 * 修改手工账日志记录
	 */
	@Override
	public Boolean updateByBo(ErpSellerStoreAccountLogBo bo) {
		ErpSellerStoreAccountLog update = BeanUtil.toBean(bo, ErpSellerStoreAccountLog.class);
		validEntityBeforeSave(update);
		return baseMapper.updateById(update) > 0;
	}

	/**
	 * 保存前的数据校验
	 */
	private void validEntityBeforeSave(ErpSellerStoreAccountLog entity) {
		// TODO 做一些数据校验,如唯一约束
	}

	/**
	 * 批量删除手工账日志记录
	 */
	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			// TODO 做一些业务上的校验,判断是否需要校验
		}
		return baseMapper.deleteBatchIds(ids) > 0;
	}

}
